package net.maku.im.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import net.maku.framework.common.exception.ServerException;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.mybatis.service.impl.BaseServiceImpl;
import net.maku.im.constant.ImConstant;
import net.maku.im.convert.ImSensitiveWordConvert;
import net.maku.im.dto.ImSensitiveWordEnabledDTO;
import net.maku.im.entity.ImSensitiveWordEntity;
import net.maku.im.dto.ImSensitiveWordQueryDTO;
import net.maku.im.vo.ImSensitiveWordVO;
import net.maku.im.dao.ImSensitiveWordDao;
import net.maku.im.service.ImSensitiveWordService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 敏感词
 *
 * @author Blue 
 * @since 1.0.0 2024-07-20
 */
@Service
@AllArgsConstructor
@DS(ImConstant.DS_IM_PLATFORM)
public class ImSensitiveWordServiceImpl extends BaseServiceImpl<ImSensitiveWordDao, ImSensitiveWordEntity> implements ImSensitiveWordService {

    @Override
    public PageResult<ImSensitiveWordVO> page(ImSensitiveWordQueryDTO query) {
        LambdaQueryWrapper<ImSensitiveWordEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(!Objects.isNull(query.getEnabled()),ImSensitiveWordEntity::getEnabled,query.getEnabled());
        wrapper.like(StrUtil.isNotEmpty(query.getContent()),ImSensitiveWordEntity::getContent,query.getContent());
        IPage<ImSensitiveWordEntity> page = baseMapper.selectPage(getPage(query), wrapper);
        return new PageResult<>(ImSensitiveWordConvert.INSTANCE.convertList(page.getRecords()), page.getTotal());
    }

    @Override
    public ImSensitiveWordVO info(Long id) {
       ImSensitiveWordEntity entity = this.getById(id);
       ImSensitiveWordVO vo = ImSensitiveWordConvert.INSTANCE.convert(entity);
       return vo;
    }

    @Override
    public void save(ImSensitiveWordVO vo) {
        this.checkExist(vo);
        ImSensitiveWordEntity entity = ImSensitiveWordConvert.INSTANCE.convert(vo);
        baseMapper.insert(entity);
    }

    @Override
    public void update(ImSensitiveWordVO vo) {
        this.checkExist(vo);
        ImSensitiveWordEntity entity = ImSensitiveWordConvert.INSTANCE.convert(vo);
        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        removeByIds(idList);
    }

    @Override
    public void switchEnabled(ImSensitiveWordEnabledDTO dto) {
        LambdaUpdateWrapper<ImSensitiveWordEntity> wrapper = Wrappers.lambdaUpdate();
        wrapper.eq(ImSensitiveWordEntity::getId,dto.getId());
        wrapper.set(ImSensitiveWordEntity::getEnabled,dto.getEnabled());
        this.update(wrapper);
    }

    private void checkExist(ImSensitiveWordVO vo){
        LambdaUpdateWrapper<ImSensitiveWordEntity> wrapper = Wrappers.lambdaUpdate();
        wrapper.eq(ImSensitiveWordEntity::getContent,vo.getContent());
        wrapper.ne(!Objects.isNull(vo.getId()),ImSensitiveWordEntity::getId,vo.getId());
        if(this.exists(wrapper)){
            throw new ServerException(String.format("敏感词‘%s’已存在",vo.getContent()));
        }
    }
}